package org.bitmap.mm.util;

import android.graphics.Bitmap;
import android.os.Handler;
import android.widget.ImageView;

/**
 * 异步更新图片
 * @author hljdrl@gmail.com
 *
 */
class LoveBitmapHandler extends Handler {
	public static final int STATE_BITMAP_SET = 1;
	protected ImageCaches mImageCache;
	protected LoveBitmapManager loveBitmapManager;

	public LoveBitmapHandler(ImageCaches caches, LoveBitmapManager love) {
		mImageCache = caches;
		loveBitmapManager = love;
	}

	public void handleMessage(android.os.Message msg) {
		switch (msg.what) {
		case STATE_BITMAP_SET:
			ImageData info = (ImageData) msg.obj;
			ImageView imageView = info.imageViewReference.get();
			Bitmap bitmap = null;
			if (imageView != null) {
				if (info.mType == ImageData.IMAGE_TYPE_THUMBNAIL) {
						bitmap = loveBitmapManager.loadLocationImage(info,  info.bitConfig);

				} else if (info.mType == ImageData.IMAGE_TYPE_NORMAL) {
					bitmap = loveBitmapManager.loadLocationImage(info, info.imageWidth, info.bitConfig);
				}
				if (bitmap != null) {
					loveBitmapManager.setImageBitmap(imageView, bitmap);
				}
			}
			break;
		}
		try {
			removeMessages(msg.what);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

}
